﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using fuzhu;
using LH.Model.PU;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NETCore.MailKit.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Ocsp;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.WMS;
using XXX.Models.XXXEntities;
using Yonyou.U8.BO;
using ZDEntity;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace XXX.api.Controllers
{

 
    /// <summary>
    /// 生成材料出库单
    /// </summary>
    [Route("api/wms/[controller]")]
        [ApiController]
        //[Authorize]
        public class ST_PanDianController : ControllerBase
        {
        private readonly ILogger<ST_PanDianController> _logger;
        private readonly WMS_PanDianVM _wMS_PanDianVM; 

            public ST_PanDianController(ILogger<ST_PanDianController> logger, WMS_PanDianVM wMS_PanDianVM)
            {
                //_invRepository = xxxRepository;
                _logger = logger;
            _wMS_PanDianVM = wMS_PanDianVM;
        }

        [HttpPost]
        [Route("Add")]
        public async Task<Models.RetModel2> Add(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);
           
            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }
            #region 接口调用间隔
            string strKey = ControllerContext.RouteData.Values["controller"].ToString() + Md5Encrypt.MD5(requestBody);

            if (CacheHelpCore.GetCacheValue(strKey) == null)
            {
                CacheHelpCore.SetChacheValue(strKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CanShu.chkRepeat);
            }
            else
            {
                throw new Exception("接口调用间隔过短");
            }
            #endregion 接口调用间隔
            try
            {
                string cErr = "";
                WMS_PanDianVM model = JsonConvert.DeserializeObject<WMS_PanDianVM>(requestBody);
                
                RetModel2 ret = new RetModel2();
                if (model == null)
                {
                    ret = DBService.ReErrObject("没有数据");
                }
                else
                { 
                    ret = model.Add();
                }
                 


                return ret;
            }
            catch (Exception ex)
            {
               
                return DBService.ReErrObject(ex.Message);
            }
        }

        [HttpPost]
        [Route("Query")]
        public async Task<Models.RetModel2> Query(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);

            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }
           
            try
            {
                string cErr = "";
                WMS_PanDianVM model = JsonConvert.DeserializeObject<WMS_PanDianVM>(requestBody);

                RetModel2 ret = new RetModel2();
                if (model == null)
                {
                    ret = DBService.ReErrObject("没有数据");
                }
                else
                {
                    ret = model.Query();
                }



                return ret;
            }
            catch (Exception ex)
            {

                return DBService.ReErrObject(ex.Message);
            }
        }

        [HttpPost]
        [Route("Del")]
        public async Task<Models.RetModel2> Del(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);

            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }

            try
            {
                string cErr = "";
                WMS_PanDianVM model = JsonConvert.DeserializeObject<WMS_PanDianVM>(requestBody);

                RetModel2 ret = new RetModel2();
                if (model == null)
                {
                    ret = DBService.ReErrObject("没有数据");
                }
                else
                {
                    ret = model.DelVouch();
                }



                return ret;
            }
            catch (Exception ex)
            {

                return DBService.ReErrObject(ex.Message);
            }
        }


        [HttpGet]
        [Route("DelBarcodeAll")]
        public async Task<Models.RetModel2> DelBarcodeAll(string cCheckCode)
        {
            

            try
            {
                string cErr = ""; 
                RetModel2 ret = new RetModel2();
                if (string.IsNullOrEmpty(cCheckCode))
                {
                    return DBService.ReErrObject("没有盘点单号");
                }
                
                    ret = _wMS_PanDianVM.DelBarcodeAll(cCheckCode);
                


                return ret;
            }
            catch (Exception ex)
            {

                return DBService.ReErrObject(ex.Message);
            }
        }

        [HttpGet]
        [Route("DelBarcode")]
        public async Task<Models.RetModel2> DelBarcode(string cCheckCode,string Barcodeno)
        {
           
            try
            {
                string cErr = ""; 
                RetModel2 ret = new RetModel2();
                if (string.IsNullOrEmpty(cCheckCode))
                {
                    return DBService.ReErrObject("没有盘点单号");
                }
                if (string.IsNullOrEmpty(Barcodeno))
                {
                    return DBService.ReErrObject("没有选择条码");
                }
               
                    ret = _wMS_PanDianVM.DelBarcode(cCheckCode, Barcodeno);
                



                return ret;
            }
            catch (Exception ex)
            {

                return DBService.ReErrObject(ex.Message);
            }
        }

        [HttpPost]
        [Route("SaveChkBarcode")]
        public async Task<Models.RetModel2> SaveChkBarcode([FromBody]  List<WMS_PanDianBarcode> model)
        {

           
            if (model == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }

            try
            {
                string cErr = "";
                

                RetModel2 ret = new RetModel2();
                if (model == null)
                {
                    ret = DBService.ReErrObject("没有数据");
                }
                _wMS_PanDianVM.chkList = model;
                _wMS_PanDianVM.cCheckCode = model[0].cCheckCode;
                ret = _wMS_PanDianVM.SaveChkBarcode();




                return ret;
            }
            catch (Exception ex)
            {

                return DBService.ReErrObject(ex.Message);
            }
        }


        [HttpPost]
        [Route("PanDianDuibi")]
        public async Task<Models.RetModel2> PanDianDuibi(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);

            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }

            try
            {
                string cErr = "";
                WMS_PanDianVM model = JsonConvert.DeserializeObject<WMS_PanDianVM>(requestBody);

                RetModel2 ret = new RetModel2();
                if (model == null)
                {
                    ret = DBService.ReErrObject("没有数据");
                }
                else
                {
                    ret = model.PanDianDuiBi();
                }



                return ret;
            }
            catch (Exception ex)
            {

                return DBService.ReErrObject(ex.Message);
            }
        }


        /// <summary>
        /// U8盘点单和条码盘点对账
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
            [HttpPost]
        [Route("U8PanDianDuibi")]
        public Models.RetModel<Rep_U8CheckVouchDuiBi> U8PanDianDuibi(JObject obj)
            {
                string requestBody = DbHelper.GetDbString(obj);

                if (requestBody == null)
                {
                    return DBService.ReError<Rep_U8CheckVouchDuiBi>("入参有误，没有数据");
                }
                try
                {
                    string cErr = "";
                Rep_U8CheckVouchDuiBi model = JsonConvert.DeserializeObject<Rep_U8CheckVouchDuiBi>(requestBody);
                    WMS_PanDianVM wMS_PanDianVM = new WMS_PanDianVM();

                    Models.RetModel<Rep_U8CheckVouchDuiBi> ret = wMS_PanDianVM.Rep_BarcodeU8PanDianDuiBi(model);


                    return ret;
                }
                catch (Exception ex)
                {

                    return DBService.ReError<Rep_U8CheckVouchDuiBi>(ex.Message);
                }


            }

        


        [HttpPost]
        [Route("PanDianUpdate")]
        public async Task<Models.RetModel2> PanDianUpdate(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);

            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }

            try
            {
                string cErr = "";
                WMS_PanDianVM model = JsonConvert.DeserializeObject<WMS_PanDianVM>(requestBody);

                RetModel2 ret = new RetModel2();
                if (model == null)
                {
                    ret = DBService.ReErrObject("没有数据");
                }
                else
                {
                    ret = await model.PanDianUpdate();
                }



                return ret;
            }
            catch (Exception ex)
            {

                return DBService.ReErrObject(ex.Message);
            }
        }
    }

         



        
     

     
   
     
    

    
    

}
